<?php
namespace app\admin\logic;
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class WorkRecordTotalLogic extends BaseLogic {

    /**
     * 获取统计查询条件
     * @param $department
     * @return array
     */
    public function get_search_wrt_maps($department){
        $maps = [];
        $maps[] = ['department','<>',''];

        if($department != '全部'){
            $maps[] = ['department','=',$department];
        }
        return $maps;
    }

    /**
     * 获取工作记录部门报表（天）查询条件
     * @param $department
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function get_search_wfdd_maps($department,$start_time,$end_time){
        $maps = [];
        $maps[] = ['department','=',$department];
        if(!empty($start_time) && !empty($end_time)){
            $start_date = date('Y-m-d',strtotime($start_time));
            $end_date = date('Y-m-d',strtotime($end_time));

            $maps[] = ['timeid','between',[$start_date,$end_date]];
        }
        return $maps;
    }

    /**
     * 计算推广工作次数
     * @param $department
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_extend_num($department,$start_time,$end_time){
        $maps = $this->get_search_wfdd_maps($department,$start_time,$end_time);

        //发布次数
        $release_num = model('wr_form_department_day')->get_sum($maps,'extend_release_num');

        //缺失次数
        $maps[] = ['extend_release_num','=',0];
        $maps[] = ['holiday','=',0];
        $miss_num = model('wr_form_department_day')->get_count($maps,'id');

        $data['extend_release_num'] = $release_num;
        $data['extend_miss_num'] = $miss_num;
        return $this->get_response(1,'Ok',$data);
    }

    /**
     * 计算工作日志次数
     * @param $department
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_daily_num($department,$start_time,$end_time){
        $maps = $this->get_search_wfdd_maps($department,$start_time,$end_time);

        //发布次数
        $release_num = model('wr_form_department_day')->get_sum($maps,'daily_release_num');

        //缺失次数
        $maps[] = ['daily_release_num','=',0];
        $maps[] = ['holiday','=',0];
        $miss_num = model('wr_form_department_day')->get_count($maps,'id');

        $data['daily_release_num'] = $release_num;
        $data['daily_miss_num'] = $miss_num;
        return $this->get_response(1,'Ok',$data);
    }

    /**
     * 计算活动开展次数
     * @param $department
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_active_num($department,$start_time,$end_time){
        $maps = $this->get_search_wfdd_maps($department,$start_time,$end_time);

        //发布次数
        $release_num = model('wr_form_department_day')->get_sum($maps,'active_release_num');

        //缺失次数
        $maps[] = ['active_release_num','=',0];
        $maps[] = ['holiday','=',0];
        $miss_num = model('wr_form_department_day')->get_count($maps,'id');

        $data['active_release_num'] = $release_num;
        $data['active_miss_num'] = $miss_num;
        return $this->get_response(1,'Ok',$data);
    }

    /**
     * 计算通知公告次数
     * @param $department
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_notice_num($department,$start_time,$end_time){
        $maps = $this->get_search_wfdd_maps($department,$start_time,$end_time);

        //发布次数
        $release_num = model('wr_form_department_day')->get_sum($maps,'notice_release_num');

        //缺失次数
        $maps[] = ['notice_release_num','=',0];
        $maps[] = ['holiday','=',0];
        $miss_num = model('wr_form_department_day')->get_count($maps,'id');

        $data['notice_release_num'] = $release_num;
        $data['notice_miss_num'] = $miss_num;
        return $this->get_response(1,'Ok',$data);
    }

    /**
     * 获取统计
     * @param $params
     * @return mixed
     */
    public function get_wrt($params){
        $maps = $this->get_search_wrt_maps($params['department']);

        $list = model('user')->get_column($maps,'DISTINCT department','','','','','',false);
        $list_new = [];
        foreach($list as $k => $v){
            $list_new[$k]['department'] = $v;

            //计算推广工作次数
            $r = $this->count_extend_num($v,$params['start_time'],$params['end_time']);
            $list_new[$k]['extend_release_num'] = $r['data']['extend_release_num'];
            $list_new[$k]['extend_miss_num'] = $r['data']['extend_miss_num'];

            //计算工作日志次数
            $r = $this->count_daily_num($v,$params['start_time'],$params['end_time']);
            $list_new[$k]['daily_release_num'] = $r['data']['daily_release_num'];
            $list_new[$k]['daily_miss_num'] = $r['data']['daily_miss_num'];

            //计算活动开展次数
            $r = $this->count_active_num($v,$params['start_time'],$params['end_time']);
            $list_new[$k]['active_release_num'] = $r['data']['active_release_num'];
            $list_new[$k]['active_miss_num'] = $r['data']['active_miss_num'];

            //计算通知公告次数
            $r = $this->count_notice_num($v,$params['start_time'],$params['end_time']);
            $list_new[$k]['notice_release_num'] = $r['data']['notice_release_num'];
            $list_new[$k]['notice_miss_num'] = $r['data']['notice_miss_num'];
        }

        $data['list'] = $list_new;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成统计excel
     * @param $ExcelName
     * @param $data
     * @return bool
     */
    public function gen_wrt_excel($ExcelName,$data){
        if(count($data[0]) > 26){
            return $this->get_response(0,'导出excel最大列数为26，请缩减数据列数');
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        for($row = 1;$row <= count($data);$row++){
            $index = 0;
            foreach($data[$row-1] as $k => $v) {
                $col = chr($index + 65);
                $cell = ($col . $row);
                $sheet->setCellValue($cell, $v);
                $index++;
            }
        }

        header('Content-Type: application/vnd.ms-excel;charset=utf8-general-ci');
        header('Content-Disposition: attachment;filename='.$ExcelName);
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //$writer->save($ExcelName);
        return true;
    }
}